local Jewel = require("app.public.dao.Jewel")

local JewelSlot = class("JewelSlot")

local string_split = string.split

function JewelSlot:ctor(jewelTyp)
    self.slotType = jewelTyp -- 宝石孔支持的属性类型

    self.slotTypeList = {} -- 宝石孔支持的属性类型列表
    if self.slotType and "" ~= self.slotType then
        self.slotTypeList = string_split( self.slotType, ";" )
    end

    self.jewel = nil

    self._smallIcon = nil -- 该宝石孔的小图标
    self._bigIcon = nil -- 该宝石孔的大图标
    self._typeDescStr = nil -- 该宝石孔的槽位类型(攻击型/生存型/混合型)
end

function JewelSlot:write(value)
    local jewel ={}
    if self.jewel then
        self.jewel:write(jewel)
    end
    value.jewel = jewel
end

function JewelSlot:read(value)
    if value.jewel then
        local jewel = Jewel.new()
        jewel:read(value.jewel)
        self.jewel = jewel
    end
end

function JewelSlot:hasJewel()
    return self.jewel ~= nil
end

-- 宝石 jewel 是否符合此槽位？
function JewelSlot:fitJewel( jewel )
    local fitFlag = false -- 默认不符合
    if self.slotType and "" ~= self.slotType then -- 此槽位能插珠宝/宝石？
        local jewelAttrTypeStr = tostring( jewel:getAttrType() ) -- jewel提供的属性
        if table.indexof( self.slotTypeList, jewelAttrTypeStr ) then
            fitFlag = true
        end
    end
    return fitFlag
end

-- 返回槽位背景小图标
function JewelSlot:getIcon()
    if not self._smallIcon then
        if self.slotType and "" ~= self.slotType then -- 此槽位能插珠宝/宝石？
            if 1 >= #self.slotTypeList then -- 单属性孔
                self._smallIcon = GD:getImagePath( Res.slotIconPath, "common_cell_12" )
            else
                local hasAttackTppe = false -- 含有攻击型定位
                local hasSurvivalTppe = false -- 含有生存型定位
                for i,v in ipairs( self.slotTypeList ) do
                    local propConfig = GD:getPropConfig( tonumber( v ) )
                    if propConfig and propConfig.insertString and "" ~= propConfig.insertString then -- 属性有类型(攻击型/生存型)说明
                        local insertStringList = string_split( propConfig.insertString, "_" )
                        if 1 == tonumber( insertStringList[2] ) then
                            hasAttackTppe = true
                        elseif 2 == tonumber( insertStringList[2] ) then
                            hasSurvivalTppe = true
                        end
                    end
                end
                if hasAttackTppe and hasSurvivalTppe then -- 生存型、进攻型都含有？
                    self._smallIcon = GD:getImagePath( Res.slotIconPath, "common_cell_13" )
                elseif hasAttackTppe then -- 单进攻型？
                    self._smallIcon = GD:getImagePath( Res.slotIconPath, "common_cell_14" )
                elseif hasSurvivalTppe then -- 单生存型？
                    self._smallIcon = GD:getImagePath( Res.slotIconPath, "common_cell_15" )
                end
            end
        end
    end
    return self._smallIcon
end

-- 返回槽位背景大图标
function JewelSlot:getBigIcon()
    if not self._bigIcon then
        if self.slotType and "" ~= self.slotType then -- 此槽位能插珠宝/宝石？
            if 1 >= #self.slotTypeList then -- 单属性孔
                self._bigIcon = GD:getImagePath( Res.slotIconPath, "common_cell_7" )
            else
                local hasAttackTppe = false -- 含有攻击型定位
                local hasSurvivalTppe = false -- 含有生存型定位
                for i,v in ipairs( self.slotTypeList ) do
                    local propConfig = GD:getPropConfig( tonumber( v ) )
                    if propConfig and propConfig.insertString and "" ~= propConfig.insertString then -- 属性有类型(攻击型/生存型)说明
                        local insertStringList = string_split( propConfig.insertString, "_" )
                        if 1 == tonumber( insertStringList[2] ) then
                            hasAttackTppe = true
                        elseif 2 == tonumber( insertStringList[2] ) then
                            hasSurvivalTppe = true
                        end
                    end
                end
                if hasAttackTppe and hasSurvivalTppe then -- 生存型、进攻型都含有？
                    self._bigIcon = GD:getImagePath( Res.slotIconPath, "common_cell_10" )
                elseif hasAttackTppe then -- 单进攻型？
                    self._bigIcon = GD:getImagePath( Res.slotIconPath, "common_cell_9" )
                elseif hasSurvivalTppe then -- 单生存型？
                    self._bigIcon = GD:getImagePath( Res.slotIconPath, "common_cell_8" )
                end
            end
        end
    end
    return self._bigIcon
end

-- 返回槽位类型(攻击型/生存型/混合型)
function JewelSlot:getTypeDesc()
    if not self._typeDescStr then
        if self.slotType and "" ~= self.slotType then -- 此槽位能插珠宝/宝石？
            if 1 >= #self.slotTypeList then -- 单属性孔
                local jwelConf = GD:queryJewelByAttrType( self.slotTypeList[1] )
                self._typeDescStr = L(jwelConf.detailedType)
            else
                local hasAttackTppe = false -- 含有攻击型定位
                local hasSurvivalTppe = false -- 含有生存型定位
                for i,v in ipairs( self.slotTypeList ) do
                    local propConfig = GD:getPropConfig( v )
                    if propConfig and propConfig.insertString and "" ~= propConfig.insertString then -- 属性有类型(攻击型/生存型)说明
                        local insertStringList = string_split( propConfig.insertString, "_" )
                        if 1 == tonumber( insertStringList[2] ) then
                            hasAttackTppe = true
                        elseif 2 == tonumber( insertStringList[2] ) then
                            hasSurvivalTppe = true
                        end
                    end
                end
                if hasAttackTppe and hasSurvivalTppe then -- 生存型、进攻型都含有？
                    self._typeDescStr = L("lua_code_text_35")
                elseif hasAttackTppe then -- 单进攻型？
                    self._typeDescStr = L("sockettype_1")
                elseif hasSurvivalTppe then -- 单生存型？
                    self._typeDescStr = L("sockettype_2")
                end
            end
        end
    end
    return self._typeDescStr
end

return JewelSlot
